define(function(require, exports, module) {

	"use strict";
	require("mui");
	require("jquery");
	var abpdata = require("abpinit");
	var abp = abpdata.abpstart;
	var template = require("template");
	var globalInit = require("globalInit"); //共用js
	var eHistory = require("eHistory");
	require('http://g.alicdn.com/ilw/ding/0.8.9/scripts/dingtalk.js');
	//返回清除缓存
	var dingTalkNav = require('dd_config');
	mui.init({
		swipeBack: true //启用右滑关闭功能
	});

	function getRightFun() {
		var popoverMore = $("header > a").attr("href");
		mui(popoverMore).popover('toggle');
	}
	var muiTips = require("muiTips"); //封装的弹框
	//调用页面权限控制接口
	var sourceType = abp.sourceType; //获取权限类型接口数据
	var authorityInit = require("operatAuthority"); //调用封装的权限方法
	var authorityData = sessionStorage.getItem('operatAuthorityData'); //获取权限数据
	var authoritySELL_OUTPUT = authorityInit.getJurisdiction(authorityData, sourceType.SELL_OUTPUT); //库存出库权限判断
	if(authoritySELL_OUTPUT.indexOf(3) === -1) {
		muiTips.alertTips(' ', '您没有操作订单的权限，请联系管理员开通吧！', 2, 3, "default");
		$(".more").remove();
		dingTalkNav.RightNavBar(false, "更多", "default");
	} else {
		$(".more").show(); //按钮显示
		dingTalkNav.RightNavBar(true, "更多", getRightFun);
	}
	//接收审批中心订单的值
	var SourceId = sessionStorage.getItem('SourceId');
	var AuditId = sessionStorage.getItem('AuditId');
	var Audit_detailId = sessionStorage.getItem('Audit_detailId');
	var SourceType = sessionStorage.getItem('SourceType');

	//接收首页订单的值
	var salesOutId = sessionStorage.getItem('salesOutId');
	if(sessionStorage.getItem('outPSourceType') && sessionStorage.getItem('outPSourceID')) {
		var outPSourceType = sessionStorage.getItem('outPSourceType');
		var outPSourceID = sessionStorage.getItem('outPSourceID');
	}
	mui('#updateSells').on('tap', 'a', function() {
		document.location.href = this.href + "?dd_nav_bgcolor=FFFF943E";
	});

	function historyAudit() {
		eHistory.funAudit(AuditId, Audit_detailId, true, $('#notes').text());
	}

	function historyNoAudit() {
		eHistory.funAudit(AuditId, Audit_detailId, false, $('#notes').text());
	}
	//更多事件绑定
	mui('body').on('tap', '.mui-popover-action li>a', function() {
		var a = this,
			parent;
		//根据点击按钮，反推当前是哪个actionsheet
		for(parent = a.parentNode; parent != document.body; parent = parent.parentNode) {
			if(parent.classList.contains('mui-popover-action')) {
				break;
			}
		}
		//关闭actionsheet
		mui('#' + parent.id).popover('toggle');
		switch($(this).text()) {
			case '重置草稿':
				muiTips.confirmTips(' ', '您确定要重置吗！', 2, 1, resetDraftInPurchase);
				break;
			case '通过':
				muiTips.alertTips(' ', '恭喜您已通过！', 1, 1, historyAudit);
				break;
			case '驳回':
				muiTips.alertTips(' ', '驳回成功！', 1, 1, historyNoAudit);
				break;
			case '编辑':

				break;
			case '删除':
				muiTips.confirmTips(' ', '您确定要删除吗！', 2, 1, deleteInPurchase);
				break;
		}
	});

	var option = {};
	var dataModel = {};
	if(sessionStorage.getItem('SourceId') && sessionStorage.getItem('AuditId') && sessionStorage.getItem('Audit_detailId') && sessionStorage.getItem('SourceType')) {
		//审批中心入口
		dataModel = {
			"id": sessionStorage.getItem('SourceId')
		};
		//初始化页面显示
		$('header > a').attr('href', '#eForMore');
		$('#fixeddiv').css('display', 'none');

		//调用审批历史数据
		setTimeout(function() {
			eHistory.historyCon('sHistoryList', '#historyList', SourceType, SourceId);
		}, 1000);
		//钉钉
		dingTalkNav.LeftNavBar(clearSessionSPDD);
		//ad
		dingTalkNav.leftAndroid(clearSessionSPDD);
		//返回清楚审批中心订单缓存
		mui('body').on('tap', 'header > button', function() {
			clearSessionSP();
		});
	} else {
		//首页入口
		dataModel = {
			"id": salesOutId
		};
		//返回清除首页订单缓存
		mui('body').on('tap', 'header > button', function() {
			clearSession();
		});

		if(sessionStorage.getItem('outPSourceType') && sessionStorage.getItem('outPSourceID')) {
			//  		$('#fixeddiv').css('display','block');
			//返回清除首页订单缓存
			mui('body').on('tap', 'header > button', function() {
				clearSession();
			});
			setTimeout(function() {
				eHistory.historyCon('sHistoryList', '#historyList', outPSourceType, outPSourceID);
			}, 1000);
		}

		//  	$('#fixeddiv').css('display','block');
		var stastusId = sessionStorage.getItem("strStatusId");
		////待入库/待出库
		if(stastusId == abp.statusId.Pending || stastusId == abp.statusId.Processing || stastusId == abp.statusId.Finish) {
			$('header > a').attr('href', '#forMore');
		} else {
			$('header > a').attr('href', '#editForMore');
		}
		//钉钉
		dingTalkNav.LeftNavBar(clearSessionDD);
		//ad
		dingTalkNav.leftAndroid(clearSessionDD);
		mui('body').on('tap', '#fixeddiv', function() {
			sessionStorage.setItem('oSouItemId', settlement());
			sessionStorage.removeItem('outPSourceType');
			sessionStorage.removeItem('outPSourceID');
			sessionStorage.removeItem('strStatusId');
			sessionStorage.removeItem('salesData');
			sessionStorage.removeItem('salesOutId');
			sessionStorage.removeItem('salesSourceType');
			document.location.href = '../salesOrder/index.html?dd_nav_bgcolor=FFFF943E';
		});

	}

	//返回清除缓存
	function clearSessionDD() {
		sessionStorage.removeItem('outPSourceType');
		sessionStorage.removeItem('outPSourceID');
		sessionStorage.removeItem('goodsIndex');
		sessionStorage.removeItem('sellId');
		sessionStorage.removeItem('setParm');
		sessionStorage.removeItem('salesData');
		sessionStorage.removeItem('salesSourceType');
		sessionStorage.removeItem('salesOutId');
		sessionStorage.removeItem('setParm');
		sessionStorage.removeItem('strStatusId');
		document.location.href = "index.html?dd_nav_bgcolor=FFFF943E";
	}
	//返回清除缓存→审批
	function clearSessionSPDD() {
		sessionStorage.removeItem('SourceId');
		sessionStorage.removeItem('AuditId');
		sessionStorage.removeItem('Audit_detailId');
		sessionStorage.removeItem('SourceType');
		document.location.href = "../Examination/index.html?dd_nav_bgcolor=FFFF943E";
	}

	//返回清除缓存
	function clearSession() {
		sessionStorage.removeItem('outPSourceType');
		sessionStorage.removeItem('outPSourceID');
		sessionStorage.removeItem('goodsIndex');
		sessionStorage.removeItem('sellId');
		sessionStorage.removeItem('setParm');
		sessionStorage.removeItem('salesData');
		sessionStorage.removeItem('salesSourceType');
		sessionStorage.removeItem('salesOutId');
		sessionStorage.removeItem('setParm');
		sessionStorage.removeItem('strStatusId');
	}
	//返回清除缓存→审批
	function clearSessionSP() {
		sessionStorage.removeItem('SourceId');
		sessionStorage.removeItem('AuditId');
		sessionStorage.removeItem('Audit_detailId');
		sessionStorage.removeItem('SourceType');
	}
	//获取货品id
	function settlement() {
		var souId = '';
		$.each($('#outPDetUL > li'), function(i, v) {
			if(i === 0) {
				souId += $(v).attr('data-id');
			} else {
				souId += ',' + $(v).attr('data-id');
			}
		});
		return souId;
	}

	abp.services.app.sellOutput.getModel(dataModel, option).
	done(function(data) {
		sessionStorage.setItem("salesData", JSON.stringify(data));
		//单据详情绑定
		$('#statusName').text(data.statusName);
		$('#statusName').attr("data-statusId", data.statusId);
		$('#serialNo').text(data.serialNo);
		$('#serialDate').text(data.serialDate.substring(0, 10));
		$('#companyName').text(data.companyName);
		$('#busUserName').text(data.busUserName);
		var texttaxRateType = '';
		switch(data.taxRateType) {
			case 1:
				texttaxRateType = '不计税';
				break;
			case 2:
				texttaxRateType = '不含税价';
				break;
			case 3:
				texttaxRateType = '含税价';
				break;
		}
		$('#taxRateType').text(texttaxRateType);
		$('#totalAmount').text(data.totalAmount);
		$('#discount').text(data.discount);
		$('#discountAmount').text(data.discountAmount);
		$('#operatorName').text(data.operatorName);
		$('#dataCreated').text(data.dataCreated.substring(0, 10));
		$('#notes').text(data.notes);
		//状态颜色
		var statusIdColor = require("colorOrder");
		statusIdColor("#statusName");
		//货品明细数据绑定
		var data1 = {};
		var strDetail = data.sellOutputDetail;
		data1.list = strDetail;
		var html = template('outPDetList', data1);
		$("#outPDetUL").html(html);
		//      	console.log(strDetail);
		for(var i = 0, j = strDetail.length; i < j; i++) {
			if(strDetail[i].sourceId && strDetail[i].sourceItemId && strDetail[i].sourceSerial && strDetail[i].sourceType) {
				$('#fixeddiv').css('display', 'block');
				sessionStorage.setItem("salesSourceType", "salesSourceType");
			}
		}

		if(data.success) {

		} else {

		}
	}).fail(function(data) {
		var muiTips = require("muiTips");
		muiTips.alertTips(' ', data.message, 2, 3, "default");
	});
	//重置草稿    
	function resetDraftInPurchase() {
		var option = {};
		var dataModel = {
			"id": salesOutId,
			"serialNo": $('#serialNo').text()
		};
		console.log(dataModel.serialNo);
		abp.services.app.sellOutput.resetDraft(dataModel, option).
		done(function(data) {
			document.location.href = "index.html?dd_nav_bgcolor=FFFF943E";
			if(data.success) {

			} else {

			}
		}).fail(function(data) {
			var muiTips = require("muiTips");
			muiTips.alertTips(' ', data.message, 2, 3, "default");
		});
	}
	//删除入库记录

	function deleteInPurchase() {
		var option = {};
		var dataModel = {
			"id": salesOutId,
			"serialNo": $('#serialNo').text()
		};
		abp.services.app.sellOutput.delete(dataModel, option).
		done(function(data) {
			document.location.href = "index.html?dd_nav_bgcolor=FFFF943E";
		}).fail(function(data) {
			var muiTips = require("muiTips");
			muiTips.alertTips(' ', data.message, 2, 3, "default");
		});
	}

	globalInit.eHistoryInit("#historyList"); //审批历史数据提示

});